package com.famnote.randomchat;


import android.app.Dialog;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;

import org.jivesoftware.smack.AccountManager;

import org.jivesoftware.smackx.muc.MultiUserChat;


/**
 * Gather the xmpp settings and create an XMPPConnection
 */
public class SettingsDialog extends Dialog implements android.view.View.OnClickListener {
    
	// Debugging
    private static final String TAG = "BcastChat";
    private static final boolean D = true;
    
    
	private GroupChat xmppClient;

    
    public SettingsDialog(GroupChat xmppClient) {
        super(xmppClient);
        this.xmppClient = xmppClient;
    }

    protected void onStart() {
        super.onStart();
        setContentView(R.layout.settings);
        getWindow().setFlags(4, 4);
        setTitle(xmppClient.getResources().getString(R.string.settings_dialog_title));
        
        /*
        EditText host = (EditText) findViewById(R.id.host);
        host.setVisibility(View.GONE);
        EditText port = (EditText) findViewById(R.id.port);
        port.setVisibility(View.GONE);
        EditText service = (EditText) findViewById(R.id.service);
        service.setVisibility(View.GONE);
        EditText userid = (EditText) findViewById(R.id.userid);
        userid.setVisibility(View.GONE);
        EditText password = (EditText) findViewById(R.id.password);
        password.setVisibility(View.GONE);
        */
        
        Button ok = (Button) findViewById(R.id.ok);
        ok.setOnClickListener(this);
        
        ////ok.setVisibility(View.GONE);
        
        
        /*
        ConnectionConfiguration connConfig =
        		new ConnectionConfiguration("push.appaas.com", 5222, "push.appaas.com");
        String username = "nevertry";
        String password = "genever";
        
        XMPPConnection connection = new XMPPConnection(connConfig);
        
        //connConfig.setSASLAuthenticationEnabled(true);
        //connConfig.setDebuggerEnabled(true);
        

        try {
            connection.connect();
            Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());
            
            
            connection.login(username, password);
            Log.i("XMPPClient", "Logged in as " + connection.getUser());

            
            // Set the status to available
            Presence presence = new Presence(Presence.Type.available);
            connection.sendPacket(presence);
            
            
            
            //String room = "private-chat-" + UUID.randomUUID().toString();
          //room = room + "@groupchat.google.com";
            String room = "hulbox";
            room = room + "@conference.push.appaas.com";
            MultiUserChat muc = new MultiUserChat(connection, room);
            muc.join(username);
            
 
            
            xmppClient.setConnection(connection);
            
        } catch (XMPPException ex) {
            Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());
            Log.e("XMPPClient", ex.toString());
            
            xmppClient.setConnection(null);
        }
        
        dismiss();
        */
        
    }

    public void onClick(View v) {
    	/*
        String host = getText(R.id.host);
        String port = getText(R.id.port);
        //String service = getText(R.id.service);
        String username = getText(R.id.userid);
        String password = getText(R.id.password);
		*/
    	String username = "aaa";
    	String password = "";
        
 
        ConnectionConfiguration connConfig =
        		new ConnectionConfiguration("push.appaas.com", 5222, "push.appaas.com");
        
        /*
        String number = null;
        String service = Context.TELEPHONY_SERVICE;
        TelephonyManager tel_manager = (TelephonyManager)xmppClient.getSystemService(service);
        //int device_type = tel_manager.getPhoneType();

        number = tel_manager.getLine1Number();
        
        if (number.substring(0,1).equals("+") == true) {
        	number = number.substring(1);
        }

        if (number.substring(0,2).equals("82") == true) {
        	number = "0" + number.substring(2);
        }

        
        username = number;
        password = "1234";
        */
        
        
        XMPPConnection connection = new XMPPConnection(connConfig);
        
        //connConfig.setSASLAuthenticationEnabled(true);
        //connConfig.setDebuggerEnabled(true);
        

        try {
            connection.connect();
            Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());
            
            //if(D) Log.e(TAG, "++ ON START ++");
            
            
            //connection.loginAnonymously();
            //connection.login(username, password);
            connection.login("creath", "1234");
            Log.i("XMPPClient", "Logged in as " + connection.getUser());

            
            /*
            // Set the status to available
            Presence presence = new Presence(Presence.Type.available);
            connection.sendPacket(presence);
            */
            
            /*
            //String room = "private-chat-" + UUID.randomUUID().toString();
          //room = room + "@groupchat.google.com";
            String room = "hulbox";
            room = room + "@conference.push.appaas.com";
            MultiUserChat muc = new MultiUserChat(connection, room);
            muc.join(connection.getUser());
            */

            
            //String room = "private-chat-" + UUID.randomUUID().toString();
            //room = room + "@groupchat.google.com";
            //room = room + "@conference.push.appaas.com";
            /*
              String room = "hulbox";
              room = room + "@conference.push.appaas.com";
              */
            //MultiUserChat muc = new MultiUserChat(connection, room);
            //muc.join(username);
            
            xmppClient.setConnection(connection);
            
            dismiss();
            
        } catch (XMPPException ex) {
            Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());
            Log.e("XMPPClient", ex.toString());
            
            xmppClient.setConnection(null);
        }
        
        
        
        
        
        /*
        // Create a connection
        //ConnectionConfiguration connConfig =
          //      new ConnectionConfiguration(host, Integer.parseInt(port), service);
        
        ConnectionConfiguration connConfig =
        		new ConnectionConfiguration("push.appaas.com", 5222, "push.appaas.com");
        username = "admin";
        password = "genever";
        
        XMPPConnection connection = new XMPPConnection(connConfig);
        
        //connConfig.setSASLAuthenticationEnabled(true);
        //connConfig.setDebuggerEnabled(true);
        

        try {
            connection.connect();
            Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());
        } catch (XMPPException ex) {
            Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());
            Log.e("XMPPClient", ex.toString());
            xmppClient.setConnection(null);
        }
        
        try {
        	
        	
            connection.login(username, password);
            Log.i("XMPPClient", "Logged in as " + connection.getUser());

            
            // Set the status to available
            Presence presence = new Presence(Presence.Type.available);
            connection.sendPacket(presence);
            xmppClient.setConnection(connection);
            
        	
        	
            
            //AccountManager manager = connection.getAccountManager();
            //boolean accountSupport = manager.supportsAccountCreation();
            //if(accountSupport){
                //manager.createAccount(userName,userPass,accouAttributes);
            //}else{
               //System.out.print("Server NotSupporting");
            	Log.e("XMPPClient", "Server NotSupporting");
            //} 
            
            
            //try {
             //   manager.createAccount("creath","genever");//username & paswd

            //} catch (XMPPException e) {
                // TODO Auto-generated catch block
                //e.printStackTrace();
            //}
            
            
        } catch (XMPPException ex) {
            Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + username);
            Log.e("XMPPClient", ex.toString());
                xmppClient.setConnection(null);
        }
        */
        
        //dismiss();
        
    }

    private String getText(int id) {
        EditText widget = (EditText) this.findViewById(id);
        return widget.getText().toString();
    }
}

